﻿namespace Calendar
{
    using System;
    using System.Threading;
    using System.Windows.Forms;

    public class DrawTool : ITool
    {
        private EventHandler _Complete;
        private Calendar.DayView m_DayView;
        private DateTime m_SelectionStart;
        private bool m_SelectionStarted;

        public event EventHandler Complete
        {
            add
            {
                EventHandler handler2;
                EventHandler complete = this._Complete;
                do
                {
                    handler2 = complete;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    complete = Interlocked.CompareExchange<EventHandler>(ref this._Complete, handler3, handler2);
                }
                while (complete != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler complete = this._Complete;
                do
                {
                    handler2 = complete;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    complete = Interlocked.CompareExchange<EventHandler>(ref this._Complete, handler3, handler2);
                }
                while (complete != handler2);
            }
        }

        public void MouseDown(MouseEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e.Button == MouseButtons.Left)
            {
                this.m_SelectionStart = this.m_DayView.GetTimeAt(e.X, e.Y);
                this.m_DayView.SelectionStart = this.m_SelectionStart;
                this.m_DayView.SelectionEnd = this.m_SelectionStart.AddMinutes((double) (60 / this.m_DayView.SlotsPerHour));
                this.m_SelectionStarted = true;
                this.m_DayView.Invalidate();
                this.m_DayView.Capture = true;
            }
        }

        public void MouseMove(MouseEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if ((e.Button == MouseButtons.Left) && this.m_SelectionStarted)
            {
                DateTime time = this.m_DayView.GetTimeAt(e.X, e.Y).AddMinutes((double) (60 / this.m_DayView.SlotsPerHour));
                if (time < this.m_SelectionStart)
                {
                    this.m_DayView.SelectionStart = time;
                    this.m_DayView.SelectionEnd = this.m_SelectionStart;
                }
                else
                {
                    this.m_DayView.SelectionEnd = time;
                }
                this.m_DayView.Invalidate();
            }
        }

        public void MouseUp(MouseEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e.Button == MouseButtons.Left)
            {
                this.m_DayView.Capture = false;
                this.m_SelectionStarted = false;
                this.m_DayView.RaiseSelectionChanged(EventArgs.Empty);
                if (this._Complete != null)
                {
                    this._Complete(this, EventArgs.Empty);
                }
            }
        }

        public void Reset()
        {
            this.m_SelectionStarted = false;
        }

        public Calendar.DayView DayView
        {
            get
            {
                return this.m_DayView;
            }
            set
            {
                this.m_DayView = value;
            }
        }
    }
}

